/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean;

import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import edu.doeacc.tms.domain.Truck;
import edu.doeacc.tms.service.dao.TruckFacade;
import edu.doeacc.tms.web.util.JsfUtil;

/**
 *
 * @author Niraj
 */
@ManagedBean(name = "truckBean")
@ViewScoped
public class TruckBean {

    @EJB
    private TruckFacade truckFacade;
    private List<Truck> trucks;
    private Truck current;
    @ManagedProperty("#{msgs}")
    ResourceBundle bundle;

    public void setBundle(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    public TruckFacade getTruckFacade() {
        return truckFacade;
    }

    public void setTruckFacade(TruckFacade truckFacade) {
        this.truckFacade = truckFacade;
    }

    public List<Truck> getTrucks() {
        if (this.trucks == null) {
            this.trucks = truckFacade.findAll();
        }
        return trucks;
    }

    public void setTrucks(List<Truck> trucks) {
        this.trucks = trucks;
    }

    public Truck getSelected() {
        if (current == null) {
            if (this.currentTruckId > -1) {
                this.current = truckFacade.find(this.currentTruckId);
            } else {
                current = new Truck();
            }
        }
        return current;
    }

    public String prepareEdit(Long id) {
        this.current = truckFacade.find(id);
        return "pretty:editTruck";
    }

    public String update() {
        try {
            truckFacade.edit(current);
            JsfUtil.addSuccessMessage("Truck Updated Successfully");
            return "pretty:listTrucks";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareCreate() {
        this.current = new Truck();
        return "pretty:addTruck";
    }

    public String create() {
        try {
            truckFacade.create(current);
            JsfUtil.addSuccessMessage("New Truck Created");
            return "pretty:listTrucks";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy(Long id) {
        try {
            truckFacade.remove(truckFacade.find(id));
            trucks = null;
            JsfUtil.addSuccessMessage("Truck deleted successfully");
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
        }
        return "pretty:listTrucks";
    }
    Long currentTruckId = -1L;

    public Long getCurrentTruckId() {
        return getSelected().getId();
    }

    public void setCurrentTruckId(Long id) {
        this.currentTruckId = id;
    }
}
